// 知识库管理
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="知识库名称" prop="title" label-width="120px">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入知识库名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="知识类型" prop="category">
        <el-select
          v-model="queryParams.category"
          clearable
          placeholder="请选择方案类型"
        >
          <el-option
            v-for="dict in dict.type.type_knowledge"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="创建日期">
        <el-date-picker
          v-model="dateRange1"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        > -->
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:user:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="知识库名称"
        align="center"
        key="title"
        prop="title"
        width="260"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="知识类型"
        align="center"
        key="categoryDictText"
        prop="categoryDictText"
        width="110"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="简 介"
        align="center"
        key="description"
        prop="description"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="状态"
        align="center"
        key="statusDictText"
        prop="statusDictText"
        width="80"
      >
        <template slot-scope="scope">
          <div :class="scope.row.status == '1'?'employ':'forbid'">
            {{ scope.row.statusDictText }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
            >编辑</el-button
          >
           <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="check(scope.row)"
            v-hasPermi="['system:user:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="enable(scope.row, '1')"
            v-hasPermi="['system:user:remove']"
            >启用</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="enable(scope.row, '2')"
            v-hasPermi="['system:user:remove']"
            >禁用</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="880px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="知识名称：" prop="title">
              <el-input
                v-model="form.title"
                placeholder="请输入内容"
                :disabled="displays"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="知识类型" prop="category">
              <el-select
                v-model="form.category"
                clearable
                placeholder="请选择方案类型"
                :disabled="displays"
              >
                <el-option
                  v-for="dict in dict.type.type_knowledge"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="知识状态:">
              <el-radio-group v-model="form.status" :disabled="displays">
                <el-radio
                  v-for="dict in dict.type.measure_status"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置顶:" prop="ifTop">
              <el-select
                v-model="form.ifTop"
                placeholder="请选择是否置顶"
                :disabled="displays"
              >
                <el-option
                  v-for="dict in dict.type.yes_no_switch"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="orderNum">
              <el-input
                v-model="form.orderNum"
                type="number"
                :min="1"
                placeholder="请输入排序"
                :disabled="displays"
                @input="handleInput"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="简 介">
              <el-input
                v-model="form.description"
                type="textarea"
                placeholder="请输入简介"
                :disabled="displays"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="封面上传" prop="coverPhoto">
              <div class="frequency">
                <!-- <FileUploadImage
                  :fileType="fileType"
                  :fileSize="5000"
                  @getaDOCtament="getaDOCtament"
                /> -->
                <ImageUpload :limit="1" @getaDOCtament="getaDOCtament" :value="form.coverPhoto"  :displays="displays" :isShowTip="false"/>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor v-model="form.content" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!displays"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
     <!-- 添加或修改知识库对话框 -->
    <el-dialog title="查看" :visible.sync="look" width="880px" append-to-body>
      <div v-html="substance" class="subCss"></div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="look=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  articleeAdd, // 添加知识
  articlePage, //知识列表
  articlePut, //修改知识
  articleDel, //删除知识
} from "@/api/knowledgeBase/index";
import { getToken } from "@/utils/auth";
import FileUploadImage from "@/components/FileUploadImage/index.vue";
export default {
  name: "User",
  dicts: [
    "sys_normal_disable",
    "sys_user_sex",
    "yes_no_switch",
    "measure_status",
    "type_knowledge",
  ],
  components: { FileUploadImage },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //禁止输入
      displays: false,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 学校选项
      schoolList: [],
      //  班级
      classList: [],
      // 是否显示弹出层
      open: false,
      look:false,
      substance:null,//查看内容
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],

      // 搜素日期范围
      dateRange1: [],
      // 量表选项
      scaleList: [],
      //量表类型
      scaleTypeList: [],
      // 角色选项
      roleOptions: [],
      objMatter: {},
      // 表单参数
      form: {
        status: "1",
        articleType:"4",//知识库
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      //   文件格式
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "png", "jpg", "jpeg"],

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleType:"4",//知识库
      },

      // 表单校验
      rules: {
        title: [{ required: true, message: "请输入知识名称", trigger: "blur" }],
        category: [
          { required: true, message: "请选择知识类型", trigger: "blur" },
        ],
     coverPhoto:[{ required: true, message: "请上传封面", trigger: "blur" }]
      },
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    muchChange(value) {
      if (value) {
        this.queryParams.category = value[value.length - 1];
      }
    },
    /** 查询测评列表 */
    getList() {
      this.loading = true;
      articlePage(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.userList.forEach((res, index) => {
          res.index = index + 1;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    handleInput() {
      this.form.orderNum = this.form.orderNum.replace(/\D/g, "");
    },
    //获取上传文件url
    getaDOCtament(urls) {
     this.form.coverPhoto=urls
      console.log("上传文件url", urls);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        status: "1",
        coverPhoto:"",
        articleType:"4",//知识库
      };
      this.dateRange = [];
      this.objMatter = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");

      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增知识库";
        this.displays = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.reset();
      this.open = true;
      this.form = row;
      if (row.status == "1") {
        this.displays = true;
      } else {
        this.displays = false;
      }
    },
    // 查看
check(row, type) {
      this.reset();
      this.look = true;
      this.substance=row.content

    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            articlePut(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            articleeAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + userIds + '"的数据项？')
        .then(function () {
          return articleDel(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 启用
    enable(row, type) {
      let text = "";
      if (type == "1") {
        text = "启用";
      } else {
        text = "禁用";
      }
 
      this.$modal
        .confirm("确认" + text + "编号为" + row.id + '"的数据项？')
        .then(function () {
          return articlePut({...row,status:type});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped lang="scss">
.frequency {
  // width: 60%;
  // padding: 5px auto;
  // border: 1px solid #d8dce5;
}
.employ{
  border: 1px solid #84d53f;
  width: 60px;
  margin: 0 auto;
  height: 20px;
  line-height: 20px;
  color:#84d53f ;
   border-radius: 5px;
}
 .forbid{
  border: 1px solid #ff453b;
  width: 60px;
  margin: 0 auto;
  height: 20px;
  line-height: 20px;
  color:#ff453b;
  border-radius: 5px;
}
.subCss{
  width: 98%;
margin: 0 auto;
}
</style>

